#! /bin/sh
dnl
dnl Caching is usually WRONG for systems with cross-mounted file systems
dnl (the cache file may correspond to a different system).  Since configure
dnl is not on a performance-critical path, go for robustness over speed.
dnl
define([AC_CACHE_LOAD], )dnl
define([AC_CACHE_SAVE], )dnl
dnl
dnl
AC_INIT()
AC_PREREQ(2.52)
AC_CONFIG_AUX_DIR([.])
 
AC_ARG_ENABLE(echo, 
[--enable-echo                      - Turn on strong echoing.  The default 
                                     is enable=no.] ,set -x)
 
AC_ARG_ENABLE(softerror, 
[--enable-softerror                 - Turn on soft error, i.e. instead of 
                                     issuing MSG_ERROR, MSG_WARN + exit 0 
                                     are used.  The default is enable=no.],
, enable_softerror=${slog2_enable_softerror:-no})

AC_ARG_ENABLE(swingcheck, 
[--enable-swingcheck                - Turn on the Swing checking code. 
                                     The default is enable=yes],
, enable_swingcheck=yes)
 
AC_ARG_ENABLE(jdb,
[--enable-jdb                       - Turn on the use of jdb to run the GUI.
                                     The default is enable=no],
,enable_jdb=no)

# Find the home directory if not specified
if test "X$srcdir" != "X." -a -s $srcdir/bin/jumpshot.in ; then
    top_srcdir_abs=$srcdir
else
    PAC_GETWD(top_srcdir_abs,./bin/jumpshot.in)
fi
 
if test -z "$top_srcdir_abs" ; then
    top_srcdir_abs=`pwd`
fi

if test ! -d "$top_srcdir_abs" ; then
    PAC_MSG_ERROR($enable_softerror, [*** $top_srcdir_abs is not a valid directory - cannot continue])
fi

AC_SUBST(top_srcdir_abs)

# Determine this package's name based on its directory name
changequote(,)dnl
tmpname="`echo $top_srcdir_abs | sed -e 's%\(.*\)/\([^/]*\)%\2%'`"
pkgname="`echo $tmpname | sed -e 'y%abcdefghijklmnopqrstuvwxyz/%ABCDEFGHIJKLMNOPQRSTUVWXYZ_%'`"
changequote([,])dnl
if test "x$pkgname" = "x" ; then
    pkgname="SLOG-2/Jumpshot-4"
fi
 
echo "Configuring $pkgname with $ac_configure_args"
#
# Cannot use JAVA_HOME here in this configure.in.
# Because JAVA_HOME is used in Blackdown's .java_wrapper in jdk117_v3
# Using JAVA_HOME here would mistakenly set some variable in .java_wrapper
# when .java_wrapper is invoked.  i.e. causes mysterious error in tests.
AC_ARG_WITH(java, 
[--with-java=JRE_TOPDIR             - The path of the top-level directory of 
                                     the Java installation.  If this option 
                                     or with-java2 is not given, configure
                                     will try to locate java for you.  Also, 
                                     with-java2 option overwrites the 
                                     with-java option to set JRE_TOPDIR.],
JRE_TOPDIR=$withval, JRE_TOPDIR="")
 
AC_ARG_WITH(java2, 
[--with-java2=JRE_TOPDIR            - The path of the top-level directory of 
                                     the Java installation.  If this option 
                                     or with-java is not given, configure will
                                     try to locate java for you.  Also, 
                                     with-java2 option overwrites the 
                                     with-java option to set JRE_TOPDIR.],
JRE_TOPDIR=$withval, )

AC_ARG_WITH(jvmflags, 
[--with-jvmflags=JVMFLAGS           - supply java virtual machine flags, e.g. 
                                     -Xms32m -Xmx256m.],
JVMFLAGS="$withval", )

AC_ARG_WITH(trace_libdir,
[--with-trace-libdir=TRACE_LIBDIR   - The path to the user-defined
                                     TRACE-API shared library, i.e.
                                     libTraceInput.so/TraceInput.dll.
                                     This option creates 2 extra utility 
                                     programs: traceprint and traceTOslog2.],
TRACE_LIBDIR=$withval, )

AC_ARG_WITH(trace_name,
[--with-trace-name=TRACE_NAME       - The new name prefix that replaces 
                                     the word trace in the utility programs
                                     traceprint and traceTOslog2.],
TRACE_NAME=$withval, )

# Fixup for make
MAKE=${MAKE:-make}
PAC_MAKE_IS_GNUMAKE
PAC_MAKE_IS_BSD44
PAC_MAKE_IS_OSF
PAC_MAKE_VPATH
# If we are relying on VPATH and no VPATH is set, then we must exit
if test ! -s bin/jumpshot.in -a -z "$VPATH" ; then
    AC_MSG_ERROR([No virtual MAKE path command found.
	You may need to set your make command.
	The GNU make (sometimes available as gnumake) can be used.])
fi
AC_SUBST(MAKE)

dnl checking for working INSTALL
AC_PROG_INSTALL
PAC_PROG_CHECK_INSTALL_WORKS
 
CPRP=cp
MKDIR=mkdir
RM="rm -f"
MV="mv -f"
AC_SUBST(RM)

# Check if --without-java is specified
if test "$JRE_TOPDIR" = "no" ; then
    PAC_MSG_ERROR($enable_softerror, [--without-java is specified, exit!])
fi

if test $enable_jdb = "yes" ; then
    pac_JVM=jdb
#   Assume jdb is working
    is_jvm_working=yes
else
    pac_JVM=java

    JAC_CHECK_CLASSPATH
    is_jvm_working=no
#
dnl JVM="$JRE_TOPDIR/bin/$pac_JVM" is used in JAC_PATH_PROG
    JAC_PATH_PROG(JVM, $pac_JVM, [
dnl Initialize the checking flag.
        jac_java_working=yes
dnl
        if test "$jac_java_working" = "yes" ; then
            AC_MSG_CHECKING([if $JVM runs simple java class])
            JAC_TRY_RUN([$JVM], [$JVMFLAGS],
                        [ac_javatest/simple.class],
                        [jac_java_working=yes], [jac_java_working=no])
            if test "$jac_java_working" = "yes" ; then
                AC_MSG_RESULT(yes)
            else
                AC_MSG_RESULT(no)
            fi
        fi
dnl
        if test "$jac_java_working" = "yes" ; then
            AC_MSG_CHECKING([if $JVM runs simple jar file])
            JAC_TRY_RUNJAR([$JVM], [$JVMFLAGS],
                           [ac_javatest/simple.jar],
                           [jac_java_working=yes], [jac_java_working=no])
            if test "$jac_java_working" = "yes" ; then
                AC_MSG_RESULT(yes)
            else
                AC_MSG_RESULT(no)
            fi
        fi
dnl
        if test "$jac_java_working" = "yes" ; then
            AC_MSG_CHECKING([if $JVM runs non-GUI tool])
            JAC_TRY_RUNJAR([$JVM], [$JVMFLAGS],
                           [ac_javatest/awt_color.jar],
                           [jac_java_working=yes], [jac_java_working=no])
            if test "$jac_java_working" = "no" ; then
                JAC_TRY_RUNJAR([$JVM], [$JVMFLAGS -Djava.awt.headless=true],
                               [ac_javatest/awt_color.jar],
                               [jac_java_working=yes], [jac_java_working=no])
                if test "$jac_java_working" = "yes" ; then
                    AC_MSG_RESULT([-Djava.awt.headless=true])
                    NONGUI_JVMFLAGS="-Djava.awt.headless=true"
                else
                    AC_MSG_RESULT(no)
                fi
            else
                AC_MSG_RESULT(yes)
            fi
        fi
dnl
        if test "$jac_java_working" = "yes" \
             -a "$enable_swingcheck" = "yes" ; then
            AC_MSG_CHECKING([if $JVM runs simple Swing class])
            JAC_TRY_RUN([$JVM], [$JVMFLAGS],
                        [ac_javatest/simple_swing.class],
                        [jac_java_working=yes], [jac_java_working=no])
            if test "$jac_java_working" = "yes" ; then
                AC_MSG_RESULT(yes)
            else
                AC_MSG_RESULT(no)
            fi
        fi
dnl
        if test "$jac_java_working" = "yes" \
              -a "$enable_swingcheck" = "yes" ; then
            AC_MSG_CHECKING([if $JVM runs simple Swing jar file])
            JAC_TRY_RUNJAR([$JVM], [$JVMFLAGS],
                           [ac_javatest/simple_swing.jar],
                           [jac_java_working=yes], [jac_java_working=no])
            if test "$jac_java_working" = "yes" ; then
                AC_MSG_RESULT(yes)
            else
                AC_MSG_RESULT(no)
            fi
        fi
dnl
        if test "$jac_java_working" = "yes" ; then
            jac_prog_working=yes
        else
            jac_prog_working=no
        fi
    ])
    AC_SUBST(NONGUI_JVMFLAGS)
#   If JVM still hasn't been found, we need to abort!
    if test "x$JVM" = "x" ; then
        PAC_MSG_ERROR($enable_softerror,
                      [No Java Virtual Machine can be found anywhere!])
    fi
    is_jvm_working=yes
fi

# CHECK the version of Java used 
# Filter out the version of JDK that causes problem
if test "$enable_jdb" = "no" ; then
    AC_MSG_CHECKING(for Java version)
    VERSION=`$JVM -fullversion 2>&1`
    case "$VERSION" in
	*1.0.*)
            AC_MSG_RESULT([$VERSION, i.e. the oldest Java])
            AC_MSG_WARN([Java $VERSION is too old to be used with $pkgname !])
            ;;
	*1.1.*)
            AC_MSG_RESULT([$VERSION, i.e. Java 1 SDK])
            AC_MSG_WARN([Java $VERSION is too old to be used with $pkgname !])
            ;;
	*)
            AC_MSG_RESULT([$VERSION, i.e. Java 2 SDK])
            ;;
    esac
fi

if test "$is_jvm_working" = "no" ; then
    PAC_MSG_ERROR($enable_softerror,
        [$pkgname can be used with Java 2 Run Time Environment,
	i.e. j2sdk 1.2.2 or newer.])
fi

AC_SUBST(JVMFLAGS)
AC_SUBST(JVM)

JAC_CHECK_CYGPATH(JPATH_START, JPATH_FINAL)
AC_SUBST(JPATH_START)
AC_SUBST(JPATH_FINAL)

#
# Naming the various build directories, don't inherit them from environment
#
rootbuild_dir=`pwd`
for dir in lib bin doc etc share sbin ; do
    dirname=${dir}build_dir
    eval dirvalue=\$"$dirname"
    eval $dirname=$rootbuild_dir/$dir
done

# VPATH build: Set the libbuild_dir
if test ! -d $libbuild_dir -a -d $srcdir/lib ; then
    echo "creating $libbuild_dir from source"
    $CPRP -pr $srcdir/lib $libbuild_dir
fi
AC_SUBST(libbuild_dir)

# VPATH: Set the binbuild_dir
if test ! -d $binbuild_dir ; then
    echo "creating directory $binbuild_dir"
    $MKDIR $binbuild_dir
fi
AC_SUBST(binbuild_dir)

# VPATH build: Set the docbuild_dir
if test ! -d $docbuild_dir -a -d $srcdir/doc ; then
    echo "creating $docbuild_dir from source"
    $CPRP -pr $srcdir/doc $docbuild_dir
    $RM -r $docbuild_dir/old
fi
AC_SUBST(docbuild_dir)

# VPATH build: Set the etcbuild_dir
if test ! -d $etcbuild_dir -a -d $srcdir/etc ; then
    echo "creating $etcbuild_dir from source"
    $CPRP -pr $srcdir/etc $etcbuild_dir
fi
AC_SUBST(etcbuild_dir)

# VPATH build: Set the sharebuild_dir
if test ! -d $sharebuild_dir ; then
    echo "creating $sharebuild_dir"
    $MKDIR $sharebuild_dir
    if test ! -d $sharebuild_dir/logfiles ; then
        echo "creating $sharebuild_dir/logfiles"
        $MKDIR $sharebuild_dir/logfiles
    fi
fi
AC_SUBST(sharebuild_dir)

# Copying $srcdir/logfiles to sharebuild_dir
if test -d $srcdir/logfiles ; then
    echo "creating sample logfiles from source"
    $CPRP -pr $srcdir/logfiles/*.clog* $sharebuild_dir/logfiles
    $CPRP -pr $srcdir/logfiles/*.slog* $sharebuild_dir/logfiles
fi

AC_SUBST(sbinbuild_dir)

# Options to disselect individual subpackage
# slog2_enable_{build|source|sample|rlog} are env. variables set by MPE2
AC_ARG_ENABLE(build,
[--enable-build                     - Build the jar files from the java source
                                     and the sample TRACE-API implementation.
                                     Java Standard Development Kit, SDK,
                                     is needed.  The default is enable=yes.],
,enable_build=${slog2_enable_build:-yes})

AC_ARG_ENABLE(source,
[--enable-source                    - Build the jar files from the java source.
                                     Java Standard Development Kit, SDK,
                                     is needed.  The default is enable=yes.],
,enable_source=${slog2_enable_source:-yes})

AC_ARG_ENABLE(sample,
[--enable-sample                    - Build sample TRACE-API implementation.
                                     Java Standard Development Kit, SDK,
                                     is needed.  The default is enable=yes.],
,enable_sample=${slog2_enable_sample:-yes})

AC_ARG_ENABLE(rlog,
[--enable-rlog                      - Build RLOG TRACE-API implementation.
                                     Java Standard Development Kit, SDK,
                                     is needed.  The default is enable=yes.],
,enable_rlog=${slog2_enable_rlog:-yes})

AC_ARG_ENABLE(clog,
[--enable-clog                      - Build CLOG related wrappers, e.g.
                                     clog2TOslog2, clog2print....
                                     The default is enable=yes.],
,enable_clog=${slog2_enable_clog:-yes})

AC_ARG_WITH(oldsource,
[--with-oldsource=OLD_SRCDIR        - Build the old jar files from the given OLD
                                     Java source and to build slog2updater.
                                     This option is ignored if --enable-source
                                     is turned off.  The option should be set
                                     with a valid directory name. e.g. src_205,
                                     then --with-oldsource=src_205.
                                     The default is no.],
,with_oldsource="")

if test "$enable_build" = "no" ; then
    enable_source=no;
    enable_sample=no;
    enable_rlog=no;
fi

# The XXX in XXX_dirname below must match with AC_ARG_ENABLE( XXX, .. ) above
source_dirname="src"
sample_dirname="trace_sample"
rlog_dirname="trace_rlog"

trial_pkgs="source sample rlog"

# The XXX in XXX_dirname below must match with AC_ARG_WITH( XXX, .. ) above
if test "$enable_source" = "yes" \
     -a "X$with_oldsource" != "X" ; then
    oldsource_dirname="$with_oldsource"
    trial_pkgs="$trial_pkgs oldsource"
fi

dnl Cleanup the subpackages\' Makefiles if they exist
AC_MSG_CHECKING( [for leftover Makefiles in subpackages] )
leftover_pkgs=""
for pkg in $trial_pkgs ; do
    dirname=${pkg}_dirname
    eval dirvalue=\$"$dirname"
    if test -s $rootbuild_dir/$dirvalue/Makefile ; then
        leftover_pkgs="$leftover_pkgs \"$dirvalue\""
        ( cd $rootbuild_dir/$dirvalue && $MAKE distclean > /dev/null 2>&1 )
        if test -f $rootbuild_dir/$dirvalue/Makefile ; then
            $RM $rootbuild_dir/$dirvalue/Makefile
        fi
    fi
done
if test "X$leftover_pkgs" = "X" ; then
    AC_MSG_RESULT( [none] )
else
    AC_MSG_RESULT( [$leftover_pkgs cleaned] )
fi

# Enable softerror for all subpackages,
# i.e. use AC_MSG_WARN instead of AC_MSG_ERROR
slog2_enable_softerror=yes
export slog2_enable_softerror
selected_pkgs=""
if test "$enable_source" = "yes" \
     -a -s $srcdir/src/configure ; then
    selected_pkgs="$selected_pkgs source"
    AC_CONFIG_SUBDIRS(src)
fi
if test "$enable_sample" = "yes" \
     -a -s $srcdir/trace_sample/configure ; then
    selected_pkgs="$selected_pkgs sample"
    AC_CONFIG_SUBDIRS(trace_sample)
fi
if test "$enable_rlog" = "yes" \
     -a -s $srcdir/trace_rlog/configure ; then
    selected_pkgs="$selected_pkgs rlog"
    AC_CONFIG_SUBDIRS(trace_rlog)
fi
if test "X$with_oldsource" != "X" \
     -a -s $srcdir/$oldsource_dirname/configure ; then
    selected_pkgs="$selected_pkgs oldsource"
    AC_CONFIG_SUBDIRS([$oldsource_dirname])
fi
# Define bindir's executables, and pass them down to sbindir's install-package
BASIC_PGMS="slog2print slog2navigator slog2filter slog2updater logconvertor jumpshot"
TRACE_PGMS="traceprint traceTOslog2"

CLOG_PGMS="clogprint clogTOslog2 clog2print clog2TOslog2"
if test "$enable_clog" = "yes" ; then
    BASIC_PGMS="$BASIC_PGMS $CLOG_PGMS"
fi

if test "x$TRACE_LIBDIR" != "x" ; then
    AC_MSG_CHECKING([for libTraceInput.so and TraceInput.dll])
    if test -f "$TRACE_LIBDIR/libTraceInput.so" \
         -o -f "$TRACE_LIBDIR/TraceInput.dll" ; then
        AC_MSG_RESULT(yes)
        AC_SUBST(TRACE_LIBDIR)
        has_tracelib=yes
    else
        AC_MSG_RESULT(no)
        has_tracelib=no
    fi
fi

PATH_EXEFILES="sbin/install-package"
EXTRA_PGMS=""
if test "$has_tracelib" = "yes" ; then
    for exefile in $BASIC_PGMS $TRACE_PGMS ; do
        PATH_EXEFILES="$PATH_EXEFILES bin/$exefile"
    done
#   define EXTRA_PGMS to be renamed TRACE_PGMS according to the TRACE_NAME
    if test "x$TRACE_NAME" != "x" -a "$TRACE_NAME" != "trace" ; then
        for oldpgm in $TRACE_PGMS ; do
            newpgm=$TRACE_NAME`echo $oldpgm | sed -e 's%trace%%'`
            EXTRA_PGMS="$EXTRA_PGMS $newpgm"
        done
    else
        EXTRA_PGMS="$TRACE_PGMS"
    fi
else
    for exefile in $BASIC_PGMS ; do
        PATH_EXEFILES="$PATH_EXEFILES bin/$exefile"
    done
fi

# EXEFILES is used in sbin/install-package.in, so EXEFILES needs updated names.
EXEFILES="$BASIC_PGMS $EXTRA_PGMS"
AC_SUBST(EXEFILES)

AC_OUTPUT(Makefile $PATH_EXEFILES)

for script in $PATH_EXEFILES ; do
    if test -f "$script" ; then
        chmod a+x $script
    else
        PAC_MSG_ERROR($enable_softerror, [$script is not a valid file - Make sure you have configured with a valid $pkgname source directory])
    fi
done

if test "$has_tracelib" = "yes" ; then
#   Rename all the scripts in bindir according to the TRACE_NAME
    if test "x$TRACE_NAME" != "x" -a "$TRACE_NAME" != "trace" ; then
        for oldpgm in $TRACE_PGMS ; do
            if test -x "bin/$oldpgm" ; then
                newpgm=$TRACE_NAME`echo $oldpgm | sed -e 's%trace%%'`
                $MV bin/$oldpgm bin/$newpgm
            fi
        done
    fi
fi

configured_pkgs=""
for pkg in $selected_pkgs ; do
    dirname=${pkg}_dirname
    eval dirvalue=\$"$dirname"
    if test -s $dirvalue/Makefile ; then
        configured_pkgs="$configured_pkgs \"$dirvalue\""
    fi
done

if test -z "$configured_pkgs" ; then
    AC_MSG_WARN([$pkgname: No sub-package has been configured])
else
    AC_MSG_RESULT([$pkgname: $configured_pkgs have been configured])
fi
